﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
namespace AnyOffice.general.email
{
    public partial class query : ModulePage
    {
        public string formatSize(long size)
        {
            return StringHelper.FormatSize(size);
        }
        public string formatStatus(int deleteFlag, bool readFlag)
        {
            if (deleteFlag == 1)
            {
                return "<img title=\"收件人已删除\" src=\"/images/email_delete.gif\">";
            }
            if (!readFlag)
            {
                return "<img title=\"收件人未读\" src=\"/images/email_close.gif\">";
            }
            return "<img title=\"收件人已读\" src=\"/images/email_open.gif\">";
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                DataTable table = new EmailBoxDao().SelectAll(base.Conn, base.User.Identity.Name);
                this.sltType.DataSource = table;
                this.sltType.DataTextField = "BoxName";
                this.sltType.DataValueField = "BoxId";
                this.sltType.DataBind();
                this.sltType.Items.Insert(0, new ListItem("收件箱", "0"));
                this.sltType.Items.Insert(1, new ListItem("发件箱", "outbox"));
            }
            if (this.Page.IsPostBack)
            {
                this.pnlMain.Visible = false;
                if (this.sltType.Value != "outbox")
                {
                    if (this.sltType.Value == "0")
                    {
                        this.litNavTitle.Text = "收件箱 - 查询结果";
                    }
                    else
                    {
                        DataTable table2 = new EmailBoxDao().SelectByBoxId(base.Conn, Convert.ToInt32(this.sltType.Value), base.User.Identity.Name);
                        if (table2.Rows.Count > 0)
                        {
                            this.litNavTitle.Text = table2.Rows[0]["BoxName"].ToString() + " - 查询结果";
                        }
                    }
                }
                else
                {
                    this.litNavTitle.Text = "发件箱 - 查询结果";
                }
                SqlCommand command = new SqlCommand();
                if (base.Conn.State == ConnectionState.Closed)
                {
                    base.Conn.Open();
                }
                command.Connection = base.Conn;
                string str = "SELECT Email.EmailId,Email.Subject,Email.Important,Email.SendTime,Email.ReadFlag,Email.DeleteFlag,Email.TotalSize,Users.UserName,Department.DeptName";
                str = str + " FROM [dbo].[Email], [dbo].[Users], [dbo].[Department] ";
                if (this.txtAttachmentName.Value != string.Empty)
                {
                    str = str + ",[dbo].[Attachment] ";
                }
                if (this.sltType.Value != "outbox")
                {
                    str = str + " WHERE Email.DeleteFlag IN (0,2) AND Email.BoxId = @BoxId ";
                    command.Parameters.Add("@BoxId", Convert.ToInt32(this.sltType.Value));
                    str = (str + " AND Email.ToId = '" + base.User.Identity.Name + "' ") + " AND Email.FromId = Users.UserId " + " AND Users.DeptId = Department.DeptId ";
                }
                else
                {
                    str = (str + " WHERE Email.DeleteFlag IN (0,1) AND Email.FromId = '" + base.User.Identity.Name + "' ") + " AND Email.ToId = Users.UserId " + " AND Users.DeptId = Department.DeptId ";
                }
                if (this.sltReadFlag.Value != string.Empty)
                {
                    str = str + " AND Email.ReadFlag = @ReadFlag ";
                    command.Parameters.Add("@ReadFlag", Convert.ToBoolean(this.sltReadFlag.Value));
                }
                if (this.txtBeginDate.Value != string.Empty)
                {
                    str = str + " AND Email.SendTime >= @BeginTime ";
                    command.Parameters.Add("@BeginTime", Convert.ToDateTime(this.txtBeginDate.Value + " 00:00:00"));
                }
                if (this.txtEndDate.Value != string.Empty)
                {
                    str = str + " AND Email.SendTime <= @EndTime ";
                    command.Parameters.Add("@EndTime", Convert.ToDateTime(this.txtEndDate.Value + " 23:59:59"));
                }
                if (this.txtUserName.Value != string.Empty)
                {
                    str = str + " AND Users.UserName = @UserName ";
                    command.Parameters.Add("@UserName", this.txtUserName.Value);
                }
                if (this.txtSubject.Value != string.Empty)
                {
                    str = str + " AND Email.Subject LIKE '%' + @Subject + '%' ";
                    command.Parameters.Add("@Subject", this.txtSubject.Value);
                }
                if (this.txtKey1.Value != string.Empty)
                {
                    str = str + " AND Email.Content LIKE '%' + @Key1 + '%' ";
                    command.Parameters.Add("@Key1", this.txtKey1.Value);
                }
                if (this.txtKey1.Value != string.Empty)
                {
                    str = str + " AND Email.Content LIKE '%' + @Key2 + '%' ";
                    command.Parameters.Add("@Key2", this.txtKey2.Value);
                }
                if (this.txtKey3.Value != string.Empty)
                {
                    str = str + " AND Email.Content LIKE '%' + @Key3 + '%' ";
                    command.Parameters.Add("@Key3", this.txtKey3.Value);
                }
                if (this.txtAttachmentName.Value != string.Empty)
                {
                    str = str + " AND (Email.AttachmentId = Attachment.AttachmentId) " + " AND (Attachment.[Name] LIKE '%' + @AttachmentName + '%') ";
                    command.Parameters.Add("@AttachmentName", this.txtAttachmentName.Value);
                }
                str = str + " ORDER BY Email.EmailId DESC";
                command.CommandText = str;
                SqlDataReader reader = command.ExecuteReader();
                if (reader.HasRows)
                {
                    this.pnlLater.Visible = true;
                    if (this.sltType.Value != "outbox")
                    {
                        this.rptInbox.DataSource = reader;
                        this.rptInbox.DataBind();
                    }
                    else
                    {
                        this.rptOutbox.DataSource = reader;
                        this.rptOutbox.DataBind();
                    }
                    reader.Close();
                }
                else
                {
                    this.litMessage.Text = base.ShowPrompt("未找到符合条件的邮件", "query.aspx");
                }
            }
        }

    }
}
